#!/bin/bash
#安装基础包
apt-get update
apt-get -y install apt-transport-https ca-certificates curl software-properties-common apt-transport-https ca-certificates wget chrony vim  net-tools bash-completion lrzsz  ipvsadm
#载入docker镜像源
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
add-apt-repository -y "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
#载入kubeadm、kubelet、kubectl镜像源
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF | tee /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get -y update
chronyc sources
#启用br_netfilter模块
cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
modprobe br_netfilter
#修改内核参数
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1 # better than modify /etc/sysctl.conf
vm.swappiness = 0
EOF
sysctl -p
sysctl --system
# #安装docker和ipvsadm
# apt-get -y install docker-ce
# mkdir -p /etc/docker
# tee /etc/docker/daemon.json <<-'EOF'
# {
#   "registry-mirrors": ["https://d98oq4en.mirror.aliyuncs.com"]
# }
# EOF
# systemctl daemon-reload
# systemctl restart docker
# #安装cri-docker
# wget http://qiniu.thinkleak.com/cri-dockerd_0.3.1.3-0.ubuntu-jammy_amd64.deb
# shasum -a 512 cri-dockerd_0.3.1.3-0.ubuntu-jammy_amd64.deb
# dpkg -i cri-dockerd_0.3.1.3-0.ubuntu-jammy_amd64.deb
# sed -i 's/fd:\/\//fd:\/\/ --pod-infra-container-image registry.aliyuncs.com\/google_containers\/pause:3.9/g' /lib/systemd/system/cri-docker.service
# systemctl daemon-reload && systemctl restart cri-docker && systemctl enable cri-docker
# systemctl status cri-docker | grep  'container-runtime-endpoint'
# 开机载入ipvs模块
cat >> /etc/modules <<EOF
ip_vs_sh
ip_vs_wrr
ip_vs_rr
ip_vs
nf_conntrack
EOF
# 安装containerd
apt-get install containerd
mkdir -p /etc/containerd
containerd config default >> /etc/containerd/config.toml
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#' /etc/containerd/config.toml
sed -i 's#sandbox_image = "registry.k8s.io/pause:3.8"#sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"#' /etc/containerd/config.toml
echo "runtime-endpoint: unix:///run/containerd/containerd.sock" >/etc/crictl.yaml
echo "image-endpoint: unix:///run/containerd/containerd.sock" >>/etc/crictl.yaml
systemctl enable containerd
systemctl restart containerd
systemctl status containerd
# 安装cri-o
export OS=xUbuntu_20.04
export VERSION=1.25
echo "deb [signed-by=/usr/share/keyrings/libcontainers-archive-keyring.gpg] https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
echo "deb [signed-by=/usr/share/keyrings/libcontainers-crio-archive-keyring.gpg] https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list

mkdir -p /usr/share/keyrings
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | gpg --dearmor -o /usr/share/keyrings/libcontainers-archive-keyring.gpg
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/Release.key | gpg --dearmor -o /usr/share/keyrings/libcontainers-crio-archive-keyring.gpg

apt-get update
apt-get install cri-o cri-o-runc
sed -i '479s/$/pause_image = "dyrnq\/pause:3.9"/' /etc/crio/crio.conf
echo "runtime-endpoint: unix:///var/run/crio/crio.sock" >/etc/crictl.yaml
echo "image-endpoint: unix:///var/run/crio/crio.sock" >>/etc/crictl.yaml
systemctl enable crio
systemctl restart crio
systemctl status crio
#安装kubectl kubelet cri-tools
# 查看一个包支持的版本
apt-cache madison kubeadm
# apt-get install -y kubeadm=1.26.3-00 kubelet=1.26.3-00 kubectl=1.26.3-00
apt install -y kubelet=1.25.11-00 kubectl=1.25.11-00
apt install -y kubelet=1.26.3-00 kubectl=1.26.3-00 cri-tools=1.26.0-00
apt-mark hold kubelet kubectl cri-tools
#kubelet开机启动
systemctl enable kubelet --now
#创建kubelet server目录
mkdir /etc/systemd/system/kubelet.service.d
tee /etc/systemd/system/kubelet.service.d/10-kubeadm.conf <<-'EOF'
# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env                     
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/sysconfig/kubelet
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
EOF
##拉取相关镜像
#kubeadm config images pull --kubernetes-version=v1.26.3 --image-repository registry.aliyuncs.com/google_containers --cri-socket unix:///var/run/containerd/containerd.sock
docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.27.4
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.27.4
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.27.4
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.27.4
docker pull registry.aliyuncs.com/google_containers/etcd:3.5.7-0
docker pull registry.aliyuncs.com/google_containers/pause:3.9
docker pull registry.aliyuncs.com/google_containers/coredns:v1.10.1
docker pull hb.sincerecloud.com/k8s/v1.26.3/kube-keepalived:v3.10
#kubectl命令自动补全
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

#可信任证书路径
/etc/ssl/certs/ca-certificates.crt

crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/kube-apiserver:v1.27.4
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/kube-scheduler:v1.27.4
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/kube-controller-manager:v1.27.4
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/kube-proxy:v1.27.4
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/etcd:3.5.7-0
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/pause:3.9
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/coredns:v1.10.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.27.4/kube-keepalived:v3.10
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/node:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/cni:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/kube-controllers:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/typha:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/kubernetesui/metrics-scraper:v1.0.8
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/kubernetesui/dashboard:v2.7.0

crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/pause:3.8
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/kube-keepalived:v3.10
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/coredns:v1.9.3
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/etcd:3.5.6-0
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/kube-apiserver:v1.25.11
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/kube-scheduler:v1.25.11
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/kube-controller-manager:v1.25.11
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/k8s/v1.25.11/kube-proxy:v1.25.11
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/node:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/cni:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/kube-controllers:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/calico/v3.24.1/typha:v3.24.1
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/kubernetesui/metrics-scraper:v1.0.8
crictl -r unix:///var/run/crio/crio.sock  pull hb.sincerecloud.com/kubernetesui/dashboard:v2.7.0